package com.admin.boot.modules.system.controller;

import com.admin.boot.common.utils.QueryGenerator;
import com.admin.boot.modules.system.entity.SysMenu;
import com.admin.boot.modules.system.entity.SysMenuPermission;
import com.admin.boot.modules.system.entity.SysRole;
import com.admin.boot.modules.system.service.ISysMenuPermissionService;

import com.admin.boot.common.utils.Result;
import com.admin.boot.modules.system.service.ISysRoleService;
import com.admin.boot.modules.system.vo.PermissionVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version v3.0
 * @Description: TODO
 * @author: admin-boot
 * @date: 2024-02-03
 */
@Api(tags = "菜单权限")
@RestController
@RequestMapping("/sysMenuPermission/sysMenuPermission")
public class SysMenuPermissionController {

    @Autowired
    private ISysMenuPermissionService sysMenuPermissionService;

    @Autowired
    private ISysRoleService sysRoleService;

    @GetMapping("/list")
    @ApiOperation(value = "菜单权限-分页列表查询", notes = "菜单权限-分页列表查询")
    public Result<?> list(SysMenuPermission sysMenuPermission,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<SysMenuPermission> queryWrapper = QueryGenerator.initQueryWrapper(sysMenuPermission);
        Page<SysMenuPermission> page = new Page<>(pageNo, pageSize);
        Page<SysMenuPermission> sysMenuPermissionPage = sysMenuPermissionService.page(page, queryWrapper);
        return Result.OK(sysMenuPermissionPage);
    }


    @PostMapping("/saveRolePermissions")
    @ApiOperation(value = "菜单权限-保存角色权限", notes = "菜单权限-保存角色权限")
    @Transactional
    public Result<?> saveRolePermissions(@RequestBody PermissionVo permissionVo) {
        String roleCode = permissionVo.getRoleCode();
        SysRole role = sysRoleService.getByCode(roleCode);
        if (role == null) {
            return Result.error("角色不存在！");
        }
        LambdaQueryWrapper<SysMenuPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenuPermission::getRoleCode,roleCode);
        sysMenuPermissionService.remove(queryWrapper);

        List<SysMenu> menuList = permissionVo.getMenuList();

        List<SysMenu> collect = menuList.stream().distinct().collect(Collectors.toList());
        for (SysMenu sysMenu : collect) {
            SysMenuPermission permission = new SysMenuPermission();
            permission.setMenuId(sysMenu.getId());
            permission.setRoleCode(roleCode);
            sysMenuPermissionService.save(permission);
        }
        return Result.OK("保存成功！");
    }


    @GetMapping("/queryList")
    @ApiOperation(value = "菜单权限-分页列表查询", notes = "菜单权限-分页列表查询")
    public Result<?> queryList(SysMenuPermission sysMenuPermission) {
        List<SysMenuPermission> list = sysMenuPermissionService.list(null);
        return Result.OK(list);
    }


    @PostMapping("/add")
    @ApiOperation(value = "菜单权限-添加", notes = "菜单权限-添加")
    public Result<?> add(@RequestBody SysMenuPermission sysMenuPermission) {
        sysMenuPermissionService.save(sysMenuPermission);
        return Result.OK("添加成功！");
    }


    @PostMapping("/edit")
    @ApiOperation(value = "菜单权限-编辑", notes = "菜单权限-编辑")
    public Result<?> edit(@RequestBody SysMenuPermission sysMenuPermission) {
        sysMenuPermissionService.updateById(sysMenuPermission);
        return Result.OK("编辑成功！");
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "菜单权限-删除", notes = "菜单权限-删除")
    public Result<?> deleteById(String id) {
        sysMenuPermissionService.removeById(id);
        return Result.OK("删除成功！");
    }

    @GetMapping("/deleteBatch")
    @ApiOperation(value = "菜单权限-批量删除", notes = "菜单权限-批量删除")
    public Result<?> deleteBatch(String ids) {
        for (String id : ids.split(",")) {
            sysMenuPermissionService.removeById(id);
        }
        return Result.OK("删除成功！");
    }

}
